
$( document ).ready(function() {
   		
	$( "#slider_pie" ).noUiSlider({range: [0, 2000], start: 1, handles: 1, step: 20, slide: function() {
	
		$("#slider_value_pie").html(Math.round($("#slider_pie").val()));
		$("#pie_submit_button").attr("disabled", false);
	}});
	
		$("#slider_value_pie").html("1");
	$('a.arrow').click(function(){
	    $('html, body').animate({
	        scrollTop: $( $.attr(this, 'href') ).offset().top
	    }, 500);
	    return false;
	});
	
	$("a[href='#bottom']").click(function() {
		  $("html, body").animate({ scrollTop: $(document).height() }, "slow");
		  return false;
		});

	$("a[href='#top']").click(function() {
		  $("html, body").animate({ scrollTop: 0 }, "slow");
		  return false;
		});

    
});

function pie_submit() {

    if ($("#yourname").val()=="")
    {
        alert("Enter your name!");
        return;
    }
	$("#thename").html($("#yourname").val());
	
	$("#pie_question_div").fadeOut( function() {
		$("#after_submit_pie").fadeIn();
	});
	
	var answer = Math.round($("#slider_pie").val());
	
	$("#user_answer_pie").html(answer + "%");
	
	report_result($("#yourname").val(), answer);
}

function report_result(name, answer) {
	
	$.post("/db_insert", {submit: "submit", name: name, answer: answer});
}

function subscribe_button() {
	
	
	var email = $("#subscribe_email").val();
	
	if (IsEmail(email)) {
	
		$.post("/db_insert", {subscribe: "subscribe", email: email});
        $.post("http://us3.api.mailchimp.com/1.3/?method=listSubscribe",{apikey: '14b7af846297a3118de74961ca680275-us3', id: '2e87047a35', email_address: email});

		$("#subscribe_form").hide();
		$("#subscrive_promise").hide();
		$("#subscribe_thankyou").fadeIn();
		
		$("#subscribe_form_bottom").hide();
		$("#subscrive_promise_bottom").hide();		
		$("#subscribe_thankyou_bottom").fadeIn();
	}
	else {
		$("#wrong_mail").html("Please insert a valid mail");
	}
}


function IsEmail(email) {
	  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	  return regex.test(email);
}


function examples()
{
    $.get("/db_select", {average: "x"}); // average - returns num
    $.get("/db_select", {middle: "x"});  // medion - returns num
    $.get("/db_select", {topten: "x"});  // top ten - returns json
}